<template>
  <div class="list-container">
    <el-form :inline="true" class="search-panel" v-model="formData">
      <el-form-item label="角色名称：">
        <el-input v-model="formData.roleName" placeholder="角色名称"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <div class="table-panel">
      <el-row class="table-operate">
  <!--      v-if="hasPerm('role.add')"-->
        <el-button type="primary" @click="addRole()" icon="el-icon-plus">添加角色</el-button>
  <!--      v-if="hasPerm('role.remove')"-->
        <el-button type="danger" @click="removeRows()">批量删除</el-button>
      </el-row>
      <el-table
        v-loading="listLoading"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          align="center"
         />
        <el-table-column
          label="序号"
          width="55"
          align="center"
          type="index">
          <!-- <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template> -->
        </el-table-column>

        <el-table-column prop="roleName" align="center" label="角色名称" width="150"/>
        <el-table-column prop="companyName" align="center" label="所属劳务公司" min-width="130" />
        <el-table-column prop="remark" align="center" label="角色描述" min-width="130" />
        <el-table-column prop="rolesNum" align="center" label="角色用户数量" width="130"/>
        <el-table-column prop="operator" align="center" label="操作人" width="130"/>
        <el-table-column prop="updateTime" align="center" label="操作时间" width="150"/>
        <!-- <el-table-column prop="isDelete" align="center" label="是否启用" width="100px">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isDeleted" active-color="#13ce66">
            </el-switch>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" fixed="right" min-width="250" align="center" >
          <template slot-scope="scope">
            <!-- <router-link :to="'/system/authorization/'+scope.row.id+'/'+scope.row.roleName"> -->
  <!--            v-if="hasPerm('role.acl')"-->
            <el-button type="text" icon="el-icon-info" @click="authorization(scope.row)">编辑权限</el-button>
            <!-- </router-link> -->
            <router-link :to="'/system/roleEdit/'+scope.row.id+'/'+scope.row.roleName">
  <!--            v-if="hasPerm('role.update')"-->
              <el-button type="text" icon="el-icon-edit">修改角色名</el-button>
            </router-link>
  <!--          v-if="hasPerm('role.remove')"-->
            <el-button type="text" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row class="page">
        <div class="page-tag">
            共{{ Math.ceil(total / limit) }}页/{{ total }}条数据
        </div>
        <el-pagination
          background
          @size-change="changeSize"
          @current-change="fetchData"
          :current-page.sync="page"
          :page-sizes="[10, 20, 50]"
          :page-size="limit"
          layout="sizes, prev, pager, next, jumper"
          :total="total"
        />
      </el-row>
    </div>
  </div>
</template>

<script>
import roleApi from '@/api/acl/role'
import corpApi from '@/api/corp.js'
export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // 讲师列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      formData: {}, // 查询表单对象
      multipleSelection: [], // 批量选择中选择的记录列表
      category:3,
      corpList:[]
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    // console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    // console.log('list mounted......')
  },

  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      // console.log(size)
      this.limit = size
      this.fetchData(1)
    },

    addRole(){
      this.$router.push({ path: '/system/addRole' })
    },
    authorization(row){
      this.$router.push({ path: '/system/authorization',query:{id:row.id,roleName:row.roleName} })
    },
    // 加载讲师列表数据
    fetchData(page = 1) {
      // console.log('翻页。。。' + page)
      // 异步获取远程数据（ajax）
      this.page = page

      roleApi.getPageList(this.page, this.limit,this.category, this.formData).then(
        response => {
          this.list = response.data.items
          this.total = response.data.items.length
          console.log(this.list);
          // 数据加载并绑定成功
          this.listLoading = false
        }
      )
    },

    getCorpList() {

    },

    // 重置查询表单
    resetData() {
      // console.log('重置查询表单')
      this.formData = {}
      this.fetchData()
    },

    // 根据id删除数据
    removeDataById(id) {
      // debugger
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // promise
        // 点击确定，远程调用ajax
        return roleApi.removeById(id)
      }).then((response) => {
        this.fetchData(this.page)
        if (response.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      // console.log('handleSelectionChange......')
      // console.log(selection)
      this.multipleSelection = selection
    },

    // 批量删除
    removeRows() {
      // console.log('removeRows......')

      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的记录!'
        })
        return
      }

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // promise
        // 点击确定，远程调用ajax
        // 遍历selection，将id取出放入id列表
        var idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item.id)
          // console.log(idList)
        })
        // 调用api
        return roleApi.removeRows(idList)
      }).then((response) => {
        this.fetchData(this.page)
        if (response.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 执行搜索
    // queryString：文本框中输入的值
    // cb：一个函数
    querySearch(queryString, cb) {
      // console.log(queryString)
      // console.log(cb)

      // teacher.selectNameByKey(queryString).then(response => {
      //   // 调用 callback 返回建议列表的数据
      //   cb(response.data.items)
      // })
    }
  }
}
</script>
